package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Code;
import com.gxa.zhonglong.service.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/25 14:35
 * Description:编码规则操作接口
 * Version: V1.0
 */
@Api(tags ="编码规则接口api")
@CrossOrigin
@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private CodeService codeService;

    @GetMapping("/generation")
    @ApiOperation("根据编码的id,找到编码规则,生成编码")
    @ApiImplicitParam(name = "id",value = "编码规则的id",required = true)
    public Map<String,Object> generation(Long id){
        final String generation = codeService.generation(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(generation);
        return jsonResult.getDataMap();
    }

    @GetMapping("/selectAll")
    @ApiOperation("查询所有编码规则信息")
    public Map<String,Object> selectAll() throws BusinessException {
        final List<Map<String, Object>> maps = codeService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改编码规则")
    public Map<String,Object> addOrUpdate(Code code) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if (code.getId()==null) {
            codeService.add(code);
            jsonResult.setMsg("添加成功!");
        }else {
            codeService.update(code);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("根据id删除编码规则")
    public Map<String,Object> del(Long id) throws BusinessException {
        codeService.del(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
